package com.ddh.codeduck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeduck.pojo.entity.SysDictData;
import com.ddh.codeduck.mapper.SysDictDataMapper;
import com.codeduck.pojo.req.SysDictDataPageReq;
import com.ddh.codeduck.service.ISysDictDataService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author ddh
 * @since 2025-07-11
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Override
    public IPage<SysDictData> sysDictDataPage(SysDictDataPageReq sysDictDataPageReq) {
        Integer current = sysDictDataPageReq.getCurrent();
        Integer pageSize = sysDictDataPageReq.getPageSize();
        String itemLabel = sysDictDataPageReq.getItemLabel();
        String itemValue = sysDictDataPageReq.getItemValue();
        String dictType = sysDictDataPageReq.getDictType();

        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(itemLabel), SysDictData::getItemLabel, itemLabel)
                .like(StringUtils.isNotBlank(itemValue), SysDictData::getItemValue, itemValue)
                .like(StringUtils.isNotBlank(dictType), SysDictData::getDictType, dictType)
                .orderByAsc(SysDictData::getSort);

        // 如果分页参数无效，则查询所有数据并封装进 IPage
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0) {
            List<SysDictData> records = list(queryWrapper);
            Page<SysDictData> page = new Page<>();
            page.setRecords(records);
            page.setTotal(records.size());
            return page;
        } else {
            Page<SysDictData> page = new Page<>(current, pageSize);
            return page(page, queryWrapper);
        }
    }

}
